package com.liuxu.topnews;

import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.HttpCookie;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class service {
    public service(){
        CookieManager manager = new CookieManager();
        //设置cookie策略，只接受与你对话服务器的cookie，而不接收Internet上其它服务器发送的cookie
        manager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);
        CookieHandler.setDefault(manager);
    }
    public List<Object[]> gettoken(String targat) throws Exception{
        String regex = "'csrfmiddlewaretoken' value='(.*?)'";
        Pattern p = Pattern.compile(regex);
        URL url = new URL(targat);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line = null;
        String csrf_token=null;
        while((line=br.readLine())!=null){
            System.out.println(line);
            Matcher buf_m = p.matcher(line);
            while (buf_m.find()) {
                csrf_token=buf_m.group(1);
            }
        }
        br.close();
        List<HttpCookie> listCookie =manager.getCookieStore().getCookies();
        HttpCookie Cookie=listCookie.get(0);
        List<Object[]> result=new ArrayList<Object[]>();
        Object one=csrf_token;
        Object two=Cookie;
        result=[one,two];
        return result;
    }
    public String getData(String targat) throws IOException {
        HttpURLConnection connection = null;
        BufferedReader reader = null;
        URL url = new URL(targat);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setConnectTimeout(8000);
        urlConnection.setRequestMethod("GET");
        urlConnection.setReadTimeout(8000);
        InputStream inputStream = urlConnection.getInputStream();
        reader = new BufferedReader((new InputStreamReader(inputStream)));
        StringBuilder builder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            builder.append(line);
        }
        return builder.toString();
    }
    public static String postData(String path,String post){
        URL url = null;
        try {
            url = new URL(path);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestProperty("Cookie",);
            httpURLConnection.setRequestMethod("POST");// 提交模式
            // 发送POST请求必须设置如下两行
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
            // 发送请求参数
            printWriter.write(post);//post的参数 xx=xx&yy=yy
            // flush输出流的缓冲
            printWriter.flush();
            //开始获取数据
            BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int len;
            byte[] arr = new byte[1024];
            while((len=bis.read(arr))!= -1){
                bos.write(arr,0,len);
                bos.flush();
            }
            bos.close();
            return bos.toString("utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getjson(String data,String word) throws JSONException {
            JSONObject jsonObject = new JSONObject(data);
            JSONArray jsonArray = jsonObject.getJSONArray("userlist");
            JSONObject jsObj =  jsonArray.getJSONObject(0);
            String result = jsObj.getString(word);
            return result;
    }
}
